<template>
	<view>
	</view>
</template>

<script>
	import apis from '@/apis.js'
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				data: {},
				datas: {},
				code: null,
				url: '',
				openId: ''
			}
		},
		onLoad() {
			this.url = window.location.href
			this.code = this.getQueryVariable('code', this.url)
			if (this.code) {
				this.getLogin()
			}
			
		},
		computed: {
			...mapState(['img_url',　'TOKEN']),
		},
		methods: {
			...mapActions(['Login']),
			getLogin() {
				uni.request({
					url: apis.weixinLogin,
					data: { code: this.code },
					method: 'get',
					success: (result) => {
						console.log(result, 'openId')
						this.openId = result.data
						this.getLoginAppid()
					},
				})
			},
			getLoginAppid() {
				uni.request({
					url: apis.loginWenxin,
					data: { openId: this.openId },
					method: 'get',
					success: (result) => {
						if (result.data.code === 200) {
							this.Login({ token:result.data.result.authorization })
							this.getSelfAuthorId()
						} else {
							uni.redirectTo({
								url: '/pages/user/login/login?openId=' + this.openId
							})
						}
					},
				})
			},
			// 获取登陆人信息
			getSelfAuthorId() {
				this.$http.get(apis.userAuthor_detail, { }).then(res => {
					if (res.data.code === 200) {
						if (res.data.result.subjectType !== null) {
							uni.switchTab({
								url: '/pages/tabbar/tabbar-1/tabbar-1'
							})
						} else {
							uni.navigateTo({
								url: '/pages/community/major/major'
							})
						}
					}
				})
			},
			/**
			 *  截取URL，获取传进来的参数值
			 */
			getQueryVariable(variable, url) {
				const query = url.substring(url.indexOf('?') + 1, url.length)
				const vars = query.split("&")
				for (let i = 0; i < vars.length; i += 1) {
					const pair = vars[i].split('=')
					if (pair[0] === variable) {
						return pair[1]
					}
				}
				return false
			},
		}
	}
</script>

<style>

</style>
